<script>
	import {
		mapMutations,
		mapGetters,
		mapState
	} from "vuex";
	import {
		debounce,
		addressData
	} from "common/utils";

	import {
		syncCart,
		updateCollection,
		updateViewed
	} from "network/user";
	export default {
		// computed: {
		// 	...mapState(["backTab"]),
		// },
		onLaunch() {
			console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')

		},
		computed: {
			...mapState(["viewedList", "shopList", "isLogin", "collectionList"]),
		},

		created() {
			if (!uni.getStorageSync('addressData')) {
				console.log('存储地址数据', uni.getStorageSync('addressData'));
				uni.setStorageSync('addressData', addressData());
			}
			this.warning = debounce(() => {
				this.error('网络异常');
			});
			this.sync();

			//更新购物车数据
			this.syncCart = debounce((data) => {
				syncCart({
					data
				}).then(res => {
					if (res[1].data == "Invalid Authorization") {
						uni.removeStorageSync('token');
						this.setLogin(false);
					}
				}).catch();
			});

			//更新收藏数据
			this.updateCollection = debounce((data) => {
				updateCollection({
					data
				}).catch();
			});

			//更新浏览记录
			this.updateViewed = debounce((data) => {
				updateViewed({
					data
				}).catch();
			});

		},
		watch: {
			//监听vuex数据对象变化以更新localStorage
			"$store.state": {
				handler() {
					this.$store.commit("syncToCartGoodList");
				},
				deep: true,
			},
			"$store.state.shopList": {
				handler() {
					if (uni.getStorageSync('token') && this.shopList) {
						this.syncCart(JSON.stringify(this.shopList));
					}
				},
				deep: true,
			},
			"$store.state.collectionList": {
				handler() {
					if (uni.getStorageSync('token') && this.collectionList) {
						this.updateCollection(JSON.stringify(this.collectionList));
					}
				},
				deep: true,
			},
			"$store.state.viewedList": {
				handler() {
					if (uni.getStorageSync('token') && this.viewedList) {
						this.updateViewed(JSON.stringify(this.viewedList));
					}
				},
				deep: true,
			},

		},
		methods: {
			...mapMutations(["setLogin"]),
			showLoading() {
				uni.showLoading({
						title: 'loading'
					}

				);

				// #ifdef MP-ALIPAY
				this._showTimer && clearTimeout(this._showTimer);

				this._showTimer = setTimeout(() => {
						this.hideLoading();
					}

					, 3000) // #endif
			}

			,
			hideLoading() {
				uni.hideLoading();
			},
			...mapMutations(["sync"]),
			addLoading() {
				this.loadingCount++;
				setTimeout(() => {
					if (this.loadingCount != 0) this.Loading = true;
				}, 150);
			},
			closeLoading() {
				this.loadingCount--;
				if (this.loadingCount === 0) {
					this.Loading = false;
				}
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';

	$tabbar-height:100rpx;

	// 设置整个项目的背景色
	page {
		background-color: rgb(242, 242, 242);
	}

	/* #endif */
	.margin-top {

		/* #ifdef MP-WEIXIN*/
		margin-top: 200rpx;
		/* #endif */

		/* #ifdef  H5  */
		margin-top: 90rpx;
		/* #endif */

		/* #ifdef  APP*/
		margin-top: calc(84rpx + var(--status-bar-height) / 2);
		/* #endif */
	}


	.example-info {
		font-size: 28rpx;
		color: #333;
		padding: 20rpx;
	}

	.fixed-top {
		position: fixed;
		z-index: 2;
		top: 0rpx;
		// margin-bottom: $nav-height;
	}

	.away-top {
		/* #ifdef MP-WEIXIN */
		margin-top: 98rpx;
		/* #endif */

		/* #ifdef APP */
		margin-top: calc(var(--status-bar-height) / 2);
		/* #endif */
	}

	.uni-input-wrapper {
		text-align: left;
	}
</style>
